﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.ShprossesBuilder.Modelling.Components;
using ITAP.ShprossesBuilder.Modelling;
using ITAP.ShprossesBuilder.Calculations;
using ITAP.ShprossesBuilder.Properties;
using ITAP.Core.Modelling;
using ITAP.Core.Calculations;

namespace ITAP.ShprossesBuilder.Operations
{
    public class OperationFixSelectedShprossConnectorAngles : IOperation
    {
        #region IOperation Members

        public string Name
        {
            get { return "Выровнять концы соединителя"; }
        }

        public OperationGroup OperationGroup
        {
            get { return OperationGroup.ShprossConnectorOperation; }
        }

        public void Execute(ModelControl modelControl)
        {
            foreach (IComponent modelInnerComponent in modelControl.ModelContainer.AllInnerComponents)
            {
                if (modelInnerComponent is ShprossConnector)
                {
                    ShprossConnector shprossConnector = modelInnerComponent as ShprossConnector;
                    if (shprossConnector.Selected)
                    {
                        int connectorIter = 0;
                        foreach (IComponent innerComponent in shprossConnector.InnerComponents)
                        {
                            if (innerComponent.GetType() == typeof(Connector))
                            {
                                Connector connector = innerComponent as Connector;
                                ModelVector shprossConnectorTangent = ModelCalculations.RotateVector(new ModelVector(1, 0), shprossConnector.Angle.Value);
                                Angle connectorAngle = ModelCalculations.GetAngleBetweenVectors(shprossConnectorTangent, connector.Primitives[0].GetTangent(0), false);
                                AngleInterval interval = shprossConnector.AllowedConnectorsAngles[connectorIter];
                                if (!interval.IsAngleInInterval(connectorAngle, shprossConnector.ConnectorAngleTolerance))
                                {
                                    Angle angleToRotate = interval.MiddleAngle - connectorAngle;
                                    connector.Rotate(angleToRotate.Value, connector.StartPoint);
                                    if (connector.EndConnectedComponent != null)
                                    {
                                        connector.EndConnectedComponent.Rotate(angleToRotate.Value, connector.StartPoint);
                                        connector.EndConnectedComponent.DisconnectFromAll();
                                    }
                                }
                                connectorIter++;
                            }
                        }
                    }
                }
            }
        }

        #endregion

        #region IOperation Members


        public System.Drawing.Image Thumbnail
        {
            get { return Resources.FixShprossConnectorsAngles.ToBitmap(); }
        }

        #endregion

        #region IOperation Members


        public bool CanUndo
        {
            get { return true; }
        }

        #endregion
    }
}
